package com.frinder.base.db;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.Builder;
import lombok.Data;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName PageWrapper
 * @Author frinder
 * @Description 分页参数组合类
 * @Date 2019/1/19 15:20
 * @Version 1.0
 */
@Data
public class PageWrapper<T> {

    public enum OrderStrategy {
        DESC,
        ASC
    }

    @Data
    @Builder
    public static class Order {
        private String field;
        private OrderStrategy strategy;
    }

    @Data
    public static class Between {
        private String field;
        private Object minValue;
        private Object maxValue;
    }

    public PageWrapper() {
    }

    public PageWrapper(int page, int size, T target) {
        this.target = target;
        this.page = page;
        this.size = size;
    }

    public PageWrapper(T target) {
        this.target = target;
    }

    private T target;

    private int page = 1;

    private int size = 10;

    private Date createTimeFrom;

    private Date createTimeTo;

    private Date updateTimeFrom;

    private Date updateTimeTo;

    private List<String> equalFields = Lists.newArrayList();

    private List<String> notEqualFields = Lists.newArrayList();

    /**
     * 使用 inFields & notInFields 时，
     * 即使前端 target 不需要其它属性，也务必使用“ "target":{} ”的方式，初始化 target，否则查询失效
     */
    private Map<String, List<Object>> inFields = Maps.newHashMap();

    private Map<String, List<Object>> notInFields = Maps.newHashMap();

    /**
     * 排序列表
     */
    private List<Order> orders = Lists.newArrayList();

    /**
     * 大于等于 & 小于等于
     */
    private Map<String, Integer> greaterThanFields = Maps.newHashMap();

    private Map<String, Integer> greaterThanOrEqualFields = Maps.newHashMap();

    private Map<String, Integer> lessThanFields = Maps.newHashMap();

    private Map<String, Integer> lessThanOrEqualFields = Maps.newHashMap();

    /**
     *
     */
    private List<Between> betweenList = Lists.newArrayList();

    public List<String> getEqualFields() {
        if (null == equalFields) {
            equalFields = Lists.newArrayList();
        }
        return equalFields;
    }

    public List<String> getNotEqualFields() {
        if (null == notEqualFields) {
            notEqualFields = Lists.newArrayList();
        }
        return notEqualFields;
    }

    public Map<String, List<Object>> getInFields() {
        if (null == inFields) {
            inFields = Maps.newHashMap();
        }
        return inFields;
    }

    public Map<String, List<Object>> getNotInFields() {
        if (null == notInFields) {
            notInFields = Maps.newHashMap();
        }
        return notInFields;
    }

    public List<Order> getOrders() {
        if (null == orders) {
            orders = Lists.newArrayList();
        }
        orders.add(Order.builder().field("id").strategy(OrderStrategy.DESC).build());
        return orders;
    }

    public Map<String, Integer> getGreaterThanFields() {
        if (null == greaterThanFields) {
            greaterThanFields = Maps.newHashMap();
        }
        return greaterThanFields;
    }

    public Map<String, Integer> getGreaterThanOrEqualFields() {
        if (null == greaterThanOrEqualFields) {
            greaterThanOrEqualFields = Maps.newHashMap();
        }
        return greaterThanOrEqualFields;
    }

    public Map<String, Integer> getLessThanFields() {
        if (null == lessThanFields) {
            lessThanFields = Maps.newHashMap();
        }
        return lessThanFields;
    }

    public Map<String, Integer> getLessThanOrEqualFields() {
        if (null == lessThanOrEqualFields) {
            lessThanOrEqualFields = Maps.newHashMap();
        }
        return lessThanOrEqualFields;
    }
}
